package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.logic.pb.RankPb;
import com.yanqu.road.logic.sort.UnionRankSort;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;
import java.util.stream.IntStream;

public class CrossGroupUnionRankListModel {

	private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

	private UnionRankSort sorter;

	private LinkedList<UnionRank> rankList;

	private int groupId;

	private boolean needSync = false;

	public int getGroupId() {
		return groupId;
	}

	public void setRankNum(int rankNum) {
		this.rankNum = rankNum;
	}

	public RankProto.UnionRankListMsg.Builder getRankListMsg() {
		return RankPb.parseUnionRankListMsg(rankList);
	}

	public LinkedList<UnionRank> getRankList() {
		return rankList;
	}

	public CrossGroupUnionRankListModel(int groupId) {
		sorter = new UnionRankSort();
		rankList = new LinkedList<>();
		this.groupId = groupId;
	}

	public void initRankList() {
		rankList = new LinkedList<>();
	}

	public void setRankList(LinkedList<UnionRank> unionRankList) {
		synchronized (this) {
			rankList = new LinkedList<>(unionRankList);
			RankListHelper.checkUnionRankListValid(rankList);       //校验数组
			rankList.sort(sorter);
			while (rankList.size() > rankNum){
				rankList.removeLast();
			}
		}
	}

	//领取排行奖励一定要用加锁的
	public int getMyRank(String unionUid) {
		synchronized (this) {
			return unlockGetMyRank(unionUid);
		}
	}

	//排行榜排名可以不加锁
	public int unlockGetMyRank(String unionUid) {
		OptionalInt result = IntStream.range(0, rankList.size())
				.filter(i -> Objects.equals(rankList.get(i).getUnionUid(), unionUid)).findFirst();
		if (result.isPresent()) {
			return result.getAsInt() + 1;
		}
		return -1;
	}

	public void removeRank(String unionUid) {
		if (StringUtils.isNullOrEmpty(unionUid)) {
			return;
		}
		synchronized (this) {
			rankList.removeIf(m -> Objects.equals(m.getUnionUid(), unionUid));
		}
	}

	/**
	 * 添加到排行
	 */
	public boolean rankChange(UnionRank unionRank) {
		if(null == unionRank){
			return false;
		}
		boolean isChange = false;
		synchronized (this) {
			int size = rankList.size();
			if (!rankList.stream().anyMatch(predicate -> Objects.equals(predicate.getUnionUid(), unionRank.getUnionUid()))) {
				if (size < rankNum) {
					rankList.add(unionRank.clone());
					isChange = true;
				} else if (rankList.getLast().getUnionBaseInfo().getTotalEarnSpeed().compareTo(unionRank.getUnionBaseInfo().getTotalEarnSpeed()) < 0) {
					UnionRank rank = rankList.getLast();
					rank.setUnionUid(unionRank.getUnionUid());
					rank.setUnionBaseInfo(unionRank.getUnionBaseInfo());
					isChange = true;
				}
			} else {
				rankList.forEach(action -> {
					if (Objects.equals(action.getUnionUid(), unionRank.getUnionUid())) {
						action.setUnionBaseInfo(unionRank.getUnionBaseInfo());
					}
				});
				isChange = true;
			}
			rankList.sort(sorter);
		}
		if(isChange){
			needSync = true;
		}
		return isChange;
	}

	public boolean isNeedSync() {
		return needSync;
	}

	public void setNeedSync(boolean needSync) {
		this.needSync = needSync;
	}
}
